package com.alsyun.jooq;

import org.jooq.codegen.GenerationTool;
import org.jooq.codegen.JavaGenerator;
import org.jooq.meta.jaxb.*;
public class JOOQGenerator extends JavaGenerator {


    private static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
    private static final String JDBC_URL = "jdbc:mysql://39.104.19.187:3306";
    private static final String JDBC_USERNAME = "roots";
    private static final String JDBC_PASSWORD = "roots";

    private static final String JOOQ_DATABASE_NAME = "org.jooq.meta.jdbc.JDBCDatabase";

    private static String GENERATOR_PATH = "/src/main/java";

    /**
     * @param schemaName 库名称
     * @param packageName 包名称
     * @param includes 生成什么表   user.*
     * @param excludes 排除表，也就是不生成规则为  test.* | user.*
     */
    public static void genTables(String schemaName, String packageName, String includes, String excludes) throws Exception {

        Jdbc jdbc = createJdbc();
        Generator generator = createGenerator(schemaName, packageName, includes, excludes);

        Configuration configuration = new Configuration()
                .withJdbc(jdbc)
                .withGenerator(generator);

        GenerationTool.generate(configuration);

    }

    private static Generator createGenerator(String schemaName, String packageName, String includes, String excludes) {

        Database database = createDatabase(schemaName, includes, excludes);
        Target target = createTarget(packageName);
        Generate generate = createGenerate();
        Strategy strategy = new Strategy();
        strategy.setName("com.alsyun.jooq.AsInDatabaseStrategy");

        return new Generator()
                .withDatabase(database)
                .withTarget(target)
                .withGenerate(generate)
                .withStrategy(strategy);
    }

    private static Jdbc createJdbc() {
        Property property = new Property();
        property.setKey("serverTimezone");
        property.setValue("UTC");

        return new Jdbc()
                .withDriver(JDBC_DRIVER)
                .withUrl(JDBC_URL)
                .withUser(JDBC_USERNAME)
                .withPassword(JDBC_PASSWORD)
                .withProperties(property);
    }

    private static Target createTarget(String packageName) {
        return new Target().withDirectory(GENERATOR_PATH).withPackageName(packageName);
    }

    private static Database createDatabase(String schemaName, String includes, String excludes) {
        Database database = new Database().withName(JOOQ_DATABASE_NAME).withInputSchema(schemaName);
        if (includes !=null && !"".endsWith(includes)) {
            database.withIncludes(includes);
        }

        if (excludes !=null && !"".endsWith(excludes)) {
            database.withExcludes(excludes);
        }
        return database;
    }

    private static Generate createGenerate() {
        Generate generate = new Generate();
        generate.withDaos(true)
                .withPojos(true)
                .withSpringAnnotations(true);
        return generate;
    }

    private static void generate() throws Exception {
        genTables("mall_hjtc365_com",
                "com.alsyun.generator.generator", "member_code", "");
    }

    public static void main(String[] args) {
        GENERATOR_PATH = JOOQGenerator.class.getResource("/").toString().
                replaceAll("/target/classes/","member_main").
                replaceAll("file:/","")+GENERATOR_PATH;
        try {
            generate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
